package com.mitest.codediff.command;


import lombok.Data;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;

/**
 * abstract jacoco mojo
 *
 * @author liwei
 * @create 2024-07-23
 **/
@Data
public abstract class AbstractJacocoMojo extends AbstractMojo {
    /**
     * Maven project.
     */
    @Parameter(property = "project", readonly = true)
    private MavenProject project;

    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;

    @Component
    private BuildPluginManager pluginManager;

    /**
     * Jacoco coverage file
     */
    @Parameter(defaultValue = "${project.build.directory}/lete-result/jacoco")
    private File jacocoDirectory;

    /**
     * File with execution data.
     */
    @Parameter(property = "jacoco.dataFile", defaultValue = "${project.build.directory}/jacoco.exec")
    private File jacocoDataFile;

    /**
     * Flag used to suppress execution.
     */
    @Parameter(property = "skip", defaultValue = "false")
    private boolean skip;

    /**
     * html报告路径
     */
    @Parameter(property = "jacoco.html", defaultValue = "")
    private String htmlPath;

    /**
     * xml报告路径
     */
    @Parameter(property = "jacoco.xml", defaultValue = "")
    private String xmlPath;

    /**
     * 代码diff结果
     */
    @Parameter(property = "jacoco.diffcode", defaultValue = "")
    private String diffCode;

    /**
     * 代码覆盖率class文件
     */
    private String classfiles;
    /**
     * 代码覆盖率代码路径
     */
    private String sourcefiles;

    protected boolean canGenerateReportRegardingDataFiles() {
        return jacocoDataFile.exists();
    }



    @Override
    public final void execute()
            throws MojoExecutionException, MojoFailureException {
        if (skip) {
            getLog().info(
                    "Skipping JaCoCo execution because property skip is set.");
            skipMojo();
            return;
        }
        executeMethod();
    }

    protected abstract void executeMethod()
            throws MojoExecutionException, MojoFailureException;

    /**
     * Skips Mojo.
     */
    protected void skipMojo() {
    }

    /**
     * @return Maven project
     */
    protected final MavenProject getProject() {
        return project;
    }

    protected final MavenSession getSession() {
        return session;
    }

    protected final BuildPluginManager getPluginManager() {
        return pluginManager;
    }

    protected void convertProjectBeans(){

    }

    protected boolean canGenerateReport(){
        return true; // 默认生成报告 暂时不做限制 预留限制
    }

    protected String getCoveragePath(){
        String basePath = project.getBasedir().getPath();
        return String.format("%s/CoverageReport", basePath);
    }
}
